package com.saifan.wyy_ov.ui.onlishop;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.ListPopupWindow;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.common.IconText;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.data.bean.CommonDataBean;
import com.saifan.wyy_ov.data.bean.MyOrderBean;

import com.saifan.wyy_ov.model.impl.GetData;

import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.RefreshLayout;

import com.saifan.wyy_ov.utils.ToastUtil;

import lomasky.ma.utils.DensityUtil;

public class MyOrderActivity extends BaseActivity {

    private Toolbar toolbar;
    private TextView tv_category;
    String[] states = new String[]{"全部", "待付款", "待配送", "配送中", "退款中", "已退款", "已完成", "已取消"};
    int catetory = 0;
    private List<MyOrderBean> myOrderBeans = new ArrayList<MyOrderBean>();
    List<MyOrderBean> subMyOrderBeans = new ArrayList<MyOrderBean>();
    protected AdapterOrder adapterOrder;
    private GetData getData;
    private CommonDataBean commonDataBean;
    private int page = 0;
    protected static final int DELETEORDER = 1234;
    public ListView listView;
    private RelativeLayout loadlay;
    Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {


            if (msg.what == DELETEORDER) {
                int orderId = msg.arg1;

                deleteOrderByOrderID(orderId);
            }
        }

        ;
    };
    private RefreshLayout refreshLayout;
    private ListPopupWindow listPopupWindow;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView();
        getData = new GetData();
        commonDataBean = new CommonDataBean();
        commonDataBean.setCustomerID(getRoomInfoBean().getWYKHDA_ZJ());
        commonDataBean.setStoreType(null);
        initPopList();
        query();
    }

    // 筛选订单
    public List<MyOrderBean> byState(int state) {
        subMyOrderBeans.clear();
        for (MyOrderBean myOrderBean : myOrderBeans) {
            if (myOrderBean.getOrderState() == state) {
                subMyOrderBeans.add(myOrderBean);
            }
        }

        return subMyOrderBeans;
    }

    public void query() {
        commonDataBean.setPage(page);
        commonDataBean.setType(catetory);
        if (myOrderBeans.isEmpty()) {
            loadlay.setVisibility(View.VISIBLE);
        }
        getData.getDataJson(this, Api.GroupBuy.MYORDER, commonDataBean, "", new ResponseData() {
            @Override
            public void onSuccess(String json) {

                List<MyOrderBean> list = new Gson().fromJson(json, new TypeToken<List<MyOrderBean>>() {
                }.getType());
                if (list != null && list.size() > 0) {
                    if (page == 0) {
                        myOrderBeans = list;
                    } else {
                        myOrderBeans.addAll(list);
                    }

                    adapterOrder.setMyOrderBeans(myOrderBeans);
//                    if (catetory == 0) {
//
//                        adapterOrder.setMyOrderBeans(myOrderBeans);
//                    } else {
//                        adapterOrder.setMyOrderBeans(byState(catetory));
//                    }

                    adapterOrder.notifyDataSetChanged();

                } else {
                    if (page == 0) {
                        myOrderBeans.clear();
                        adapterOrder.setMyOrderBeans(myOrderBeans);
                        ToastUtil.showToast(MyOrderActivity.this, "没有数据");
                    } else {
                        ToastUtil.showToast(MyOrderActivity.this, "没有数据了");
                        page--;
                    }
                }
                loadlay.setVisibility(View.GONE);
                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);

            }

            @Override
            public void onFailed(String str) {
                loadlay.setVisibility(View.GONE);
                refreshLayout.setRefreshing(false);
                refreshLayout.setLoading(false);

            }
        });
    }

    private void initPopList() {
        listPopupWindow = new ListPopupWindow(this);
        lomasky.ma.xui.CommonAdapter adapter = new lomasky.ma.xui.CommonAdapter<IconText>(this, R.layout.pop_item) {
            @Override
            public void onBind(lomasky.ma.xui.ViewHolderHelper holder, IconText iconText, int i) {
                holder.setImage(R.id.pop_img, iconText.icon);
                holder.setText(R.id.pop_text, iconText.text);
            }
        };
        List<IconText> list = new ArrayList<>();
        list.add(new IconText(R.mipmap.order_all, "全部"));
        list.add(new IconText(R.mipmap.order_dfk, "待付款"));
        list.add(new IconText(R.mipmap.order_dps, "待配送"));
        list.add(new IconText(R.mipmap.order_psz, "配送中"));
        list.add(new IconText(R.mipmap.order_tkz, "退款中"));
        list.add(new IconText(R.mipmap.order_ytk, "已退款"));
        list.add(new IconText(R.mipmap.order_ywc, "已完成"));
        list.add(new IconText(R.mipmap.order_yqx, "已取消"));
        adapter.setData(list);

        listPopupWindow.setAdapter(adapter);
        listPopupWindow.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv_category.setText(states[position]);
                catetory = position;
                page = 0;
                listPopupWindow.dismiss();
                query();


            }
        });

    }

    private void setContentView() {
        setContentView(R.layout.activity_my_order);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        tv_category = (TextView) findViewById(R.id.tv_category);
        loadlay = (RelativeLayout) findViewById(R.id.loading_lay);
        loadlay.setVisibility(View.VISIBLE);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        listView = (ListView) findViewById(R.id.listView);
        refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        adapterOrder = new AdapterOrder(MyOrderActivity.this, myOrderBeans, handler);
        listView.setAdapter(adapterOrder);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                // TODO Auto-generated method stub
                page = 0;
                refreshLayout.setRefreshing(true);
                query();
            }
        });

        refreshLayout.setOnLoadListener(new RefreshLayout.OnLoadListener() {
            @Override
            public void onLoad() {
                // TODO Auto-generated method stub
                refreshLayout.setLoading(true);
                page++;
                query();

            }
        });
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

            }
        });

        tv_category.setText(states[catetory]);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.order_menu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.menu_more) {
            listPopupWindow.setAnchorView(toolbar);
            listPopupWindow.setWidth(DensityUtil.getWidth(this) / 2);
            listPopupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
            listPopupWindow.setHorizontalOffset(DensityUtil.getWidth(this) / 2);
            listPopupWindow.show();

        } else {
        }
        return super.onOptionsItemSelected(item);
    }

    ;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == DELETEORDER) {
            int OrderID = data.getIntExtra("OrderID", -1);
            deleteOrderByOrderID(OrderID);
        }

    }

    public void deleteOrderByOrderID(int OrderID) {
        for (int i = 0; i < myOrderBeans.size(); i++) {
            if (myOrderBeans.get(i).getID() == OrderID) {
                myOrderBeans.remove(i);
                adapterOrder.setMyOrderBeans(myOrderBeans);
                adapterOrder.notifyDataSetChanged();
                break;
            }

        }
    }

}
